package com.sgam.mf.helper;

import java.text.DecimalFormat;

public class RomanianNumberToWords {

	 private static final String[] tensNames = {
		    "",
		    " zece",
		    " douazeci",
		    " treizeci",
		    " patruzeci",
		    " cincizeci",
		    " saizeci",
		    " saptezeci",
		    " optzeci",
		    " nouazeci"
		  };

		  private static final String[] numNames = {
		    "",
		    " unu",
		    " doi",
		    " trei",
		    " patru",
		    " cinci",
		    " sase",
		    " sapte",
		    " opt",
		    " noua",
		    " zece",
		    " unsprezece",
		    " doisprezece",
		    " treisprezece",
		    " patrusprezece",
		    " cincisprezece",
		    " saisprezece",
		    " saptesprezece",
		    " optsprezece",
		    " nouasprezece"
		  };

		  private static String convertLessThanOneThousand(int number) {
			    String soFar;

			    if (number % 100 < 20){
			      soFar = numNames[number % 100];
			      number /= 100;
			    }
			    else {
			      soFar = numNames[number % 10];
			      number /= 10;

			      soFar = tensNames[number % 10] + soFar;
			      number /= 10;
			    }
			    if (number == 0) return soFar;
			    if (number == 1) return " una suta" + soFar; // Una sute  
			    if (number == 2) return " doua sute" + soFar; // Doua sute
			    return numNames[number] + " sute" + soFar; // 2,3,4,5... sute
			  }


			  public static String convert(long number) {
			    // 0 to 999 999 999 999
			    if (number == 0) { return "zero"; }

			    String snumber = Long.toString(number);

			    // pad with "0"
			    String mask = "000000000000";
			    DecimalFormat df = new DecimalFormat(mask);
			    snumber = df.format(number);

			    // XXXnnnnnnnnn 
			    int billions = Integer.parseInt(snumber.substring(0,3));
			    
			    // nnnXXXnnnnnn
			    int millions  = Integer.parseInt(snumber.substring(3,6));
			    
			    // nnnnnnXXXnnn
			    int hundredThousands = Integer.parseInt(snumber.substring(6,9)); 
			    
			    // nnnnnnnnnXXX
			    int thousands = Integer.parseInt(snumber.substring(9,12));    

			    String tradBillions;
			    switch (billions) {
			    case 0:
			      tradBillions = "";
			      break;
			    case 1 :
			      tradBillions = " un bilion ";
			      break;
			    case 2 :
				      tradBillions = " doua bilioane ";
				  break;			      
			    default :
			      tradBillions = convertLessThanOneThousand(billions) 
			      + " bilioane ";
			    }
			    String result =  tradBillions;

			    String tradMillions;
			    switch (millions) {
			    case 0:
			      tradMillions = "";
			      break;
			    case 1 :
			      tradMillions = " un milion ";
			      break;
			    case 2 :
				      tradMillions = " doua milioane ";
				   break;			      
			    default :
			      tradMillions = convertLessThanOneThousand(millions) 
			      + " milioane ";
			    }
			    result =  result + tradMillions;

			    String tradHundredThousands;
			    switch (hundredThousands) {
			    case 0:
			      tradHundredThousands = "";
			      break;
			    case 1 :
			      tradHundredThousands = " una mie ";
			      break;
			    case 2 :
				      tradHundredThousands = " doua mii ";
				  break;			      
			    default :
			      tradHundredThousands = convertLessThanOneThousand(hundredThousands) 
			      + " mii ";
			    }
			    result =  result + tradHundredThousands;

			    String tradThousand;
			    tradThousand = convertLessThanOneThousand(thousands) ;
//			    switch (thousands) {
//			    case 0:
//			    	tradThousand = "";
//			      break;
//			    case 1 :
//			    	tradThousand = " unaa mie ";
//			      break;
//			    case 2 :
//			    	tradThousand = " douaa mii ";
//				  break;			      
//			    default :
//			    	tradThousand = convertLessThanOneThousand(thousands) 
//			      + " mmii ";
//			    }
			    
			    result =  result + tradThousand;

			    // remove extra spaces!
			    return result.replaceAll("^\\s+", "").replaceAll("\\b\\s{2,}\\b", " ");
			  }
			  
			  
		  
	public static String moneyToText(Float floatSuma) {
		return "Nouasprezece lei nouazeci bani";
	}
	
	 /**
	   * testing
	   * @param args
	   */
	  public static void main(String[] args) {
//	    System.out.println("*** " + RomanianNumberToWords.convert(0));
//	    System.out.println("*** " + RomanianNumberToWords.convert(1));
//	    System.out.println("*** " + RomanianNumberToWords.convert(16));
//	    System.out.println("*** " + RomanianNumberToWords.convert(100));
//	    System.out.println("*** " + RomanianNumberToWords.convert(118));
//	    System.out.println("*** " + RomanianNumberToWords.convert(200));
//	    System.out.println("*** " + RomanianNumberToWords.convert(219));
//	    System.out.println("*** " + RomanianNumberToWords.convert(300));
//	    System.out.println("*** " + RomanianNumberToWords.convert(301));
//	    System.out.println("*** " + RomanianNumberToWords.convert(302));
//	    System.out.println("*** " + RomanianNumberToWords.convert(321));
//	    System.out.println("*** " + RomanianNumberToWords.convert(322));
//	    System.out.println("*** " + RomanianNumberToWords.convert(800));
//	    System.out.println("*** " + RomanianNumberToWords.convert(801));
		
		System.out.println("*** " + RomanianNumberToWords.convert(10000));
		System.out.println("*** " + RomanianNumberToWords.convert(10001));
		System.out.println("*** " + RomanianNumberToWords.convert(10002));
		System.out.println("*** " + RomanianNumberToWords.convert(11100));
		System.out.println("*** " + RomanianNumberToWords.convert(11101));
		System.out.println("*** " + RomanianNumberToWords.convert(11102));
	    System.out.println("*** " + RomanianNumberToWords.convert(11200));
	    System.out.println("*** " + RomanianNumberToWords.convert(11201));
	    System.out.println("*** " + RomanianNumberToWords.convert(11202));
	    System.out.println("*** " + RomanianNumberToWords.convert(20001));
	    System.out.println("*** " + RomanianNumberToWords.convert(20218));
	    System.out.println("*** " + RomanianNumberToWords.convert(21100));
		System.out.println("*** " + RomanianNumberToWords.convert(21101));
		System.out.println("*** " + RomanianNumberToWords.convert(21102));
	    System.out.println("*** " + RomanianNumberToWords.convert(21200));
	    System.out.println("*** " + RomanianNumberToWords.convert(31201));
	    System.out.println("*** " + RomanianNumberToWords.convert(31202));
	    System.out.println("*** " + RomanianNumberToWords.convert(33016));
	    System.out.println("*** " + RomanianNumberToWords.convert(35055));
	    
//	    System.out.println("*** " + RomanianNumberToWords.convert(30555));
//	    System.out.println("*** " + RomanianNumberToWords.convert(31005));
//	    System.out.println("*** " + RomanianNumberToWords.convert(31101));
//	    System.out.println("*** " + RomanianNumberToWords.convert(32100));
//	    System.out.println("*** " + RomanianNumberToWords.convert(32201));
//	    System.out.println("*** " + RomanianNumberToWords.convert(1000000));
//	    System.out.println("*** " + RomanianNumberToWords.convert(2000000));
//	    System.out.println("*** " + RomanianNumberToWords.convert(3000200));
//	    System.out.println("*** " + RomanianNumberToWords.convert(700000));
//	    System.out.println("*** " + RomanianNumberToWords.convert(9000000));
//	    System.out.println("*** " + RomanianNumberToWords.convert(9001000));
//	    System.out.println("*** " + RomanianNumberToWords.convert(123456789));
//	    System.out.println("*** " + RomanianNumberToWords.convert(2147483647));
//	    System.out.println("*** " + RomanianNumberToWords.convert(3000000010L));

	    /*
	     *** zero
	     *** one
	     *** sixteen
	     *** one hundred
	     *** one hundred eighteen
	     *** two hundred
	     *** two hundred nineteen
	     *** eight hundred
	     *** eight hundred one
	     *** one thousand three hundred sixteen
	     *** one million 
	     *** two millions 
	     *** three millions two hundred
	     *** seven hundred thousand 
	     *** nine millions 
	     *** nine millions one thousand 
	     *** one hundred twenty three millions four hundred 
	     **      fifty six thousand seven hundred eighty nine
	     *** two billion one hundred forty seven millions 
	     **      four hundred eighty three thousand six hundred forty seven
	     *** three billion ten
	     **/
	  }
}
